<?php

	/**
	 * @see Bike_Js_VarPart_Method
	 */
	require_once 'Bike/Js/VarPart/Method.php';
	
	
	class Bike_Js_VarPart_Func extends Bike_Js_VarPart_Method
	{
		const PROPERTY_ACCESSOR_PREFIX = "PROPERTY::";
		
		static protected $_partOffset = null;

		protected $_part = 'function';
		protected $_body = '';

		static public function getPropertyAccessor($Name)
		{
			return self::js()->Expr(self::PROPERTY_ACCESSOR_PREFIX.$Name);
		}

		public function _toString()
		{
			//$bodyStr = $this->jsonEncode($this->_prepareValue($this->getBody()));
			$bodyStr = $this->_prepareValue($this->getBody());
			$separator1 = '';
			$separator2 = '';
			if($this->isDebug())
			{
				$separator1 = "\n";
				$separator2 = "\n\t";
				$bodyStr = str_replace($separator1, $separator2, $bodyStr).$separator1;
			}
			return (string) $this->_processProperties(parent::_toString().$separator1.'{'.$separator2.$bodyStr.'}');
		}

		public function getOmitSettings()
		{
			return array_merge(parent::getOmitSettings(), array('params'));
		}

		public function _body($Body)
		{
			return $this->setBody($Body);
		}
		
		public function _appendBody($Body)
		{
			$args = func_get_args();
			foreach($args as $n => $Body)
			{
				$args[$n] = $this->js()->Glue->_append($Body);
			}
			return call_user_func_array(array($this, 'setBody'), $args);
		}
		
		public function _prependBody($Body)
		{
			$args = func_get_args();
			foreach($args as $n => $Body)
			{
				$args[$n] = $this->js()->Glue->_prepend($Body);
			}
			return call_user_func_array(array($this, 'setBody'), $args);
		}

		public function _param($Name, $Value)
		{
			if(is_null($this->params))
			{				$this->params = $this->js()->Base->_getObj();
			}
			$this->params->$Name = $Value;
			return $this;
		}

		public function getParam($Name)
		{
			$methodName = 'isset'.ucfirst($Name);
			if(!$this->params instanceof Bike_Js_Base || !$this->params->$methodName())
			{				require_once 'Bike/Js/Exception.php';
				throw new Bike_Js_Exception('Undefined param name');
			}
			return $this->params->$Name;
		}

		public function processGetBody($Value)
		{			if(!is_array($Value))
			{
				$Value = array($Value);
			}
			return $Value;
		}

		public function getBody()
		{
			$body = $this->processGetBody($this->_body);
			if(!$body instanceof Bike_Js_CodeContainer && $body != '')
			{
				$body = $this->js()->CodeContainer($body);
			}
			return $body;
		}

		public function setBody($Body)
		{			$args = func_get_args();
			foreach($args as $Body)
			{
				$Body = $this->_glue($Body, $this->_body);
				if(!$Body instanceof Bike_Js_CodeContainer)
				{
					$Body = $this->js()->CodeContainer($Body);
				}
			}
			$this->_body = $Body;
			return $this;
		}

		protected function _processProperties($Value)
		{
            preg_match_all("/".self::PROPERTY_ACCESSOR_PREFIX."([\w\d]+)/is", $Value, $matches);
            if(is_array($matches) && sizeof($matches[1]))
            {
            	$searchArr = array();
	            $replaceArr = array();
	            foreach($matches[1] as $i => $property)
	            {
	            	try
	            	{
		            	$searchStr = $matches[0][$i];
		            	//$replaceArr[$searchStr] = $this->jsonEncode($this->_prepareValue($this->getParam($property)));
		            	$replaceArr[$searchStr] = $this->_prepareValue($this->getParam($property));
		            	$searchArr[$searchStr] = $searchStr;
		       		}
		       		catch(Bike_Js_Exception $e)
		       		{
		       		}
	            }
	            $Value = str_replace($searchArr, $replaceArr, $Value);
	        }
            return $Value;
		}
	}
